//
//  ImageSettingCell.m
//  danpin
//
//  Created by chuangjia on 23/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "ImageSettingCell.h"

#import "ZUIImageView.h"

@interface ImageSettingCell ()
{
    
}

@property(nonatomic,strong)UIImageView *shaidanView;
@property(nonatomic,strong)UIImageView *lineView;

@end
@implementation ImageSettingCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor whiteColor];
        _imagesView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _imagesView.layer.borderColor=UIColorFromRGB(0xcccccc).CGColor;
        _imagesView.layer.borderWidth=0.5;
        _imagesView.backgroundColor = UIColorFromRGB(0xffffff);
        _imagesView.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_imagesView];

        //
        UITapGestureRecognizer* singleRecognizer2= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom2)];
        _shaidanView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _shaidanView.userInteractionEnabled=YES;
        [_shaidanView addGestureRecognizer:singleRecognizer2];
        [self.contentView addSubview:_shaidanView];
        singleRecognizer2=nil;
        //
        UITapGestureRecognizer* singleRecognizer4= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom4)];
        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _lineView.userInteractionEnabled=YES;
        [_lineView addGestureRecognizer:singleRecognizer4];
        [self.contentView addSubview:_lineView];
        singleRecognizer4=nil;
    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.imagesView.image=nil;
    self.imagesView=nil;
    self.shaidanView=nil;
    self.lineView=nil;
//    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model other:(id)other key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.data=model;
    self.other=other;
    self.key=key;
    self.indexPath=indexPath;
    [self showInfo:model key:key indexPath:indexPath];
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.indexPath=indexPath;
    self.key = key;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    
    CGFloat fTop =0;
   
    
    NSString * strURL = [[NSString stringWithFormat:@"%@", [dict objectForKey:@"url"]] replaceNullString];
    CGFloat H =0;
    if (strURL && strURL.length>0) {
        BOOL need = false;
        CGFloat w =[[dict objectForKey:@"width"] floatValue];
        CGFloat h =[[dict objectForKey:@"height"] floatValue];
        if (w>0 && h>0) {
            CGFloat fullW =DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2;
            CGFloat ratio = fullW / w;
            H = ratio * h;
        }
        else
        {
            H = DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2;
            
            need = YES;
        }
        _imagesView.frame =CGRectMake(PHOTO_FRAME_WIDTH,fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, H);
        _imagesView.backgroundColor =[UIColor clearColor];
        __block UIActivityIndicatorView *activityIndicator=nil;
        __weak UIImageView *weakImageView = _imagesView;
        
        [weakImageView sd_setImageWithURL:[NSURL URLWithString:strURL] placeholderImage:nil options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            if (!activityIndicator)
            {
                [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                activityIndicator.backgroundColor=[UIColor clearColor];
                activityIndicator.center = CGPointMake(weakImageView.center.x, weakImageView.center.y);
                [activityIndicator startAnimating];
            }
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (error) {
                image=[UIImage imageNamed:@"listcellimage.png"];
                weakImageView.image=[UIImage imageNamed:@"listcellimage.png"];
            }
            
            CGFloat fImageSize = DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2;
            CGFloat w =image.size.width;
            CGFloat h =image.size.height;
            [self.data setValue:[NSString stringWithFormat:@"%.f",w] forKey:@"width"];
            [self.data setValue:[NSString stringWithFormat:@"%.f",h] forKey:@"height"];
            
            CGFloat ratio = fImageSize / w;
            CGFloat x=0;
            if(ratio <= 1)
            {
                w =fImageSize;
                h = h*ratio;
            }
            if(need || ratio > 1)
            {
                if(w > h)
                {
                    
                    CGFloat r = h / w;
                    if(w > fImageSize)
                        w = fImageSize;
                    
                    h = w * r;
                }
                else
                {
                    CGFloat r = w / h;
                    if(h > fImageSize)
                        h = fImageSize;
                    
                    w = h * r;
                }
                x = (fImageSize-w)/2;
            }
            
            weakImageView.frame = CGRectMake(PHOTO_FRAME_WIDTH,fTop, w, h);
            int is_Top =[[dict objectForKey:@"isTop"] intValue];
            if (is_Top==0) {
                _shaidanView.frame =CGRectMake(DEVICE_WIDTH-PHOTO_FRAME_WIDTH-widthAuto(85),weakImageView.frame.origin.y, widthAuto(85),hightAuto(35));
                _shaidanView.image =[UIImage imageNamed:@"swfm.png"];
            }else
            {
                _shaidanView.frame =CGRectMake(DEVICE_WIDTH-PHOTO_FRAME_WIDTH-widthAuto(85),weakImageView.frame.origin.y, widthAuto(85), hightAuto(35));
                _shaidanView.image =[UIImage imageNamed:@"yswfm.png"];
            }
            int is_shaidan =[[dict objectForKey:@"isSelected"] intValue];
            if (is_shaidan==0)
            {
                _lineView.frame =CGRectMake(PHOTO_FRAME_WIDTH,weakImageView.frame.origin.y+weakImageView.frame.size.height- hightAuto(42), widthAuto(42), hightAuto(42));
                _lineView.image =[UIImage imageNamed:@"wxz.png"];
            }else{
                
                _lineView.frame =CGRectMake(PHOTO_FRAME_WIDTH,weakImageView.frame.origin.y+weakImageView.frame.size.height- hightAuto(42), widthAuto(42), hightAuto(42));
                _lineView.image =[UIImage imageNamed:@"yxz.png"];
            }
            [weakImageView.layer addAnimation:[CATransition animation] forKey:kCATransition];
            [activityIndicator stopAnimating];
            [activityIndicator removeFromSuperview];
            activityIndicator = nil;
        }];
        
    }

    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}

-(void)handleSingleTapFrom2
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_PTop,nil,self.data,self.key,self.indexPath);
    }
}
-(void)handleSingleTapFrom4
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_PSelectd,nil,self.data,self.key,self.indexPath);
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary*)model;
    
    CGFloat w =[[dict objectForKey:@"width"] floatValue];
    CGFloat h =[[dict objectForKey:@"height"] floatValue];
    CGFloat H =0;
    if (w>0 && h>0) {
        CGFloat fullW =DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2;
        CGFloat ratio = fullW / w;
        H = ratio * h;
    }else{
        H=DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2;
    }
    
    return H;
}
@end
